﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

using SimpleText.Framework;

public partial class LanguageVersioningPage : SimpleTextPage
{


  protected void Page_Load(object sender, EventArgs e)
  {
		InitPage();

		if  ( ! IsPostBack)
		{
			AddNewProjectBtn.Attributes.Add("onclick", "if ( ! validateNewLocalForm()) { return false;	}");
			LoadExternalProjectBtn.Attributes.Add("onclick", "if ( ! validateOpenExternalForm()) { return false;	}");
		}

		//local
		int localFilePathsListed = 0;
		string[] filePaths = Directory.GetFiles(Server.MapPath("~/App_Data/Projects"));
		if (filePaths.Length == 0)
		{
			LocalProjectsPlh.Visible = false;
			NoLocalProjectsPlh.Visible = true;
		}
		else
		{
			List<ProjectSummary> projects = new List<ProjectSummary>();
			foreach (string filePath in filePaths)
			{
				if (File.Exists(filePath))
				{
					ProjectSummary summaryToAdd = new ProjectSummary();
					summaryToAdd.FileFullPath = filePath;
					summaryToAdd.ProjectName = GetProjectName(filePath);
					projects.Add(summaryToAdd);
					if (filePath.IndexOf("sample.xml") == -1)
					{
						localFilePathsListed++;
					}
				}
			}
			LocalProjectsRpt.DataSource = projects;
			LocalProjectsRpt.DataBind();
		}

		//external
		int externalFilePathsListed = 0;
		string[] externalFilePaths = GetRecentProjectExternalFilePaths(); //TODO ### get from cookie
		if (externalFilePaths.Length == 0)
		{
			ExternalProjectsPlh.Visible = false;
			NoExternalProjectsPlh.Visible = true;
		}
		else
		{
			List<ProjectSummary> projects = new List<ProjectSummary>();
			foreach (string filePath in externalFilePaths)
			{
				if (File.Exists(filePath))
				{
					ProjectSummary summaryToAdd = new ProjectSummary();
					summaryToAdd.FileFullPath = filePath;
					summaryToAdd.ProjectName = GetProjectName(filePath);
					projects.Add(summaryToAdd);
					externalFilePathsListed++;
				}
			}
			ExternalProjectsRpt.DataSource = projects;
			ExternalProjectsRpt.DataBind();
		}
	}

	protected void LoadExternalProjectClicked(object sender, EventArgs e)
	{
		string fullPath = ExternalProjectFilePathTxt.Value;
		if (String.IsNullOrEmpty(fullPath))
		{
			return;
		}
		if (!File.Exists(fullPath))
		{
			LoadExternalProjectErrorPlh.Visible = true;
			LoadExternalProjectErrorLit.Text = "<div class=\"warning\">File not found</div>";
		}
		else
		{
			LoadExternalProjectErrorPlh.Visible = false;
			SetCurrentModel(fullPath);
			AddRecentProjectExternalFilePath(fullPath);

			Response.Redirect("~/?msg=externalprojectloaded");
		}
	}


	protected void AddNewProjectClicked(object sender, EventArgs e)
	{
		string newFilePath = Server.MapPath("~/App_Data/projects/" + NewProjectFileNameTxt.Value + ".xml");
		string fileToCopyPath = Server.MapPath("~/App_Data/templates/projecttemplate.xml");

		string newProjectName = NewProjectNameTxt.Value;

		if (File.Exists(newFilePath))
		{
			throw new ApplicationException("A project already exists at " + newFilePath);
		}
		File.Copy(fileToCopyPath, newFilePath);

		SetCurrentModel(newFilePath);
		CurrentModel.ProjectName = newProjectName;
		SaveModel(CurrentModel, newFilePath);
		
		Response.Redirect("~/?msg=newprojectadded");
	}

	
		


}
